<?php

// 会员表

namespace app\console\model;

use think\Db;
use think\Model;
use think\Request;

class Member extends Model
{
    protected static function init()
    {

        self::event('before_write', function ($data) {
            $find = Db::name('share_member')->where('member_id',$data->id)->find();
            if(empty($find)){ //没有就创建
                Db::name('share_member')->insert(['member_id' => $data->id,'username' => $data->username,'manage_id' => $data->salesman_id,'create_time' => date('Y-m-d H:i:s',time())]);
            }else{
                Db::name('share_member')->where('member_id',$data->id)->update(['manage_id' => $data->salesman_id]);
            }
        });
    }

    // 会员表 会员附属表 关联
    public function MemberInfo()
    {
        return $this->hasOne('MemberInfo', 'member_id', 'id')->field('member_id,name');
    }






    // 会员表 会员分销表 关联
    public function MemberDistribution()
    {
        return $this->hasOne('MemberDistribution', 'referrals_id', 'id')->field('distribution_id,referrals_id');
    }







    protected static function disposePassword($password)
    {
        if ($password)
            return mcs($password);
        else
            return self::where(['id' => Request::instance()->param('id')])->value('password');
    }
}